<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\services\user;

use app\dao\user\UserMoneyDao;
use app\model\agent\AgentBankCard;
use app\model\agent\AgentMoneyLog;
use app\model\order\StoreOrderItem;
use app\model\user\User;
use app\model\user\UserExtract;
use app\model\user\UserMoney;
use app\services\BaseServices;
use app\services\order\StoreOrderServices;
use Carbon\Carbon;
use crmeb\exceptions\AdminException;
use crmeb\exceptions\ApiException;
use think\facade\Db;

class UserMoneyServices extends BaseServices
{
    /**
     * 用户记录模板
     * @var array[]
     */
    protected $incomeData = [
        'pay_product' => [
            'title' => '余额支付购买商品',
            'type' => 'pay_product',
            'mark' => '余额支付{%num%}元购买商品',
            'status' => 1,
            'pm' => 0
        ],
        'pay_product_refund' => [
            'title' => '商品退款',
            'type' => 'pay_product_refund',
            'mark' => '订单退款到余额{%num%}元',
            'status' => 1,
            'pm' => 1
        ],
        'system_add' => [
            'title' => '系统增加余额',
            'type' => 'system_add',
            'mark' => '系统增加{%num%}余额',
            'status' => 1,
            'pm' => 1
        ],
        'system_sub' => [
            'title' => '系统减少余额',
            'type' => 'system_sub',
            'mark' => '系统扣除{%num%}余额',
            'status' => 1,
            'pm' => 0
        ],
        'user_recharge' => [
            'title' => '用户充值余额',
            'type' => 'recharge',
            'mark' => '成功充值余额{%price%}元,赠送{%give_price%}元',
            'status' => 1,
            'pm' => 1
        ],
        'user_recharge_refund' => [
            'title' => '用户充值退款',
            'type' => 'recharge_refund',
            'mark' => '退款扣除余额{%num%}元',
            'status' => 1,
            'pm' => 0
        ],
        'brokerage_to_nowMoney' => [
            'title' => '佣金提现到余额',
            'type' => 'extract',
            'mark' => '佣金提现到余额{%num%}元',
            'status' => 1,
            'pm' => 1
        ],
        'lottery_use_money' => [
            'title' => '参与抽奖使用余额',
            'type' => 'lottery_use',
            'mark' => '参与抽奖使用{%num%}余额',
            'status' => 1,
            'pm' => 0
        ],
        'lottery_give_money' => [
            'title' => '抽奖中奖赠送余额',
            'type' => 'lottery_add',
            'mark' => '抽奖中奖赠送{%num%}余额',
            'status' => 1,
            'pm' => 1
        ],
        'register_system_add' => [
            'title' => '新用户注册赠送余额',
            'type' => 'register_system_add',
            'mark' => '新用户注册赠送{%num%}余额',
            'status' => 1,
            'pm' => 1
        ],
    ];

    /**
     * UserMoneyServices constructor.
     * @param UserMoneyDao $dao
     */
    public function __construct(UserMoneyDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 写入用户记录
     * @param string $type 写入类型
     * @param int $uid
     * @param int|string|array $number
     * @param int|string $balance
     * @param $linkId
     * @return bool|mixed
     */
    public function income(string $type, int $uid, $number, $balance, $linkId)
    {
        $data = $this->incomeData[$type] ?? null;
        if (!$data) {
            return true;
        }
        $data['uid'] = $uid;
        $data['balance'] = $balance ?? 0;
        $data['link_id'] = $linkId;
        if (is_array($number)) {
            $key = array_keys($number);
            $key = array_map(function ($item) {
                return '{%' . $item . '%}';
            }, $key);
            $value = array_values($number);
            $data['number'] = $number['number'] ?? 0;
            $data['mark'] = str_replace($key, $value, $data['mark']);
        } else {
            $data['number'] = $number;
            $data['mark'] = str_replace(['{%num%}'], $number, $data['mark']);
        }
        $data['add_time'] = time();

        return $this->dao->save($data);
    }

    /**
     * 余额记录
     * @param $where
     * @return array
     */
    public function balanceList($where)
    {
        $status = [];
        foreach ($this->incomeData as $value) {
            $status[$value['type']] = $value['title'];
        }
        [$page, $limit] = $this->getPageValue();
        $list = $this->dao->getList($where, $page, $limit);
        //关联用户
        /** @var UserServices $userServices */
        $userServices = app()->make(UserServices::class);
        $uids = array_column($list, 'uid');
        $nicknameArr = $userServices->getColumn([['uid', 'in', $uids]], 'nickname', 'uid');
        //关联订单
        /** @var StoreOrderServices $orderServices */
        $orderServices = app()->make(StoreOrderServices::class);
        /** @var UserRechargeServices $rechargeServices */
        $rechargeServices = app()->make(UserRechargeServices::class);
        foreach ($list as &$item) {
            $item['nickname'] = $nicknameArr[$item['uid']];
            if ($item['type'] == 'pay_product' || $item['type'] == 'pay_product_refund') {
                $item['relation'] = $orderServices->value(['id' => $item['link_id']], 'order_id');
            } elseif ($item['type'] == 'recharge' || $item['type'] == 'recharge_refund') {
                $item['relation'] = $rechargeServices->value(['id' => $item['link_id']], 'order_id');
            } else {
                $item['relation'] = $status[$item['type']];
            }
            $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
            $item['type_name'] = $status[$item['type']];
        }
        $count = $this->dao->count($where);
        return compact('list', 'count', 'status');
    }

    /**
     * 余额记录备注
     * @param $data
     * @return bool
     */
    public function recordRemark($id, $mark)
    {
        if ($this->dao->update($id, ['mark' => $mark])) {
            return true;
        } else {
            throw new AdminException(100025);
        }
    }

    /**
     * 余额统计基础
     * @param $where
     * @return array
     */
    public function getBasic($where)
    {
        /** @var UserServices $userServices */
        $userServices = app()->make(UserServices::class);
        $data['now_balance'] = $userServices->sum(['status' => 1], 'now_money', true);
        $data['add_balance'] = $this->dao->sum([
            ['pm', '=', 1],
            ['type', 'in', ['system_add', 'recharge', 'extract', 'lottery_add', 'register_system_add']]
        ], 'number', false);
        $data['sub_balance'] = bcsub($data['add_balance'], $data['now_balance'], 2);
        return $data;
    }

    /**
     * 余额趋势
     * @param $where
     * @return array
     */
    public function getTrend($where)
    {
        $time = explode('-', $where['time']);
        if (count($time) != 2) throw new AdminException(100100);
        $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
        $data = [];
        if ($dayCount == 1) {
            $data = $this->trend($time, 0);
        } elseif ($dayCount > 1 && $dayCount <= 31) {
            $data = $this->trend($time, 1);
        } elseif ($dayCount > 31 && $dayCount <= 92) {
            $data = $this->trend($time, 3);
        } elseif ($dayCount > 92) {
            $data = $this->trend($time, 30);
        }
        return $data;
    }

    /**
     * 余额趋势
     * @param $time
     * @param $num
     * @param false $excel
     * @return array
     */
    public function trend($time, $num, $excel = false)
    {
        if ($num == 0) {
            $xAxis = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'];
            $timeType = '%H';
        } elseif ($num != 0) {
            $dt_start = strtotime($time[0]);
            $dt_end = strtotime($time[1]);
            while ($dt_start <= $dt_end) {
                if ($num == 30) {
                    $xAxis[] = date('Y-m', $dt_start);
                    $dt_start = strtotime("+1 month", $dt_start);
                    $timeType = '%Y-%m';
                } else {
                    $xAxis[] = date('m-d', $dt_start);
                    $dt_start = strtotime("+$num day", $dt_start);
                    $timeType = '%m-%d';
                }
            }
        }
        $time[1] = date("Y-m-d", strtotime("+1 day", strtotime($time[1])));
        $point_add = array_column($this->dao->getBalanceTrend($time, $timeType, 'add_time', 'sum(number)', 'add'), 'num', 'days');
        $point_sub = array_column($this->dao->getBalanceTrend($time, $timeType, 'add_time', 'sum(number)', 'sub'), 'num', 'days');
        $data = $series = [];
        foreach ($xAxis as $item) {
            $data['余额积累'][] = isset($point_add[$item]) ? floatval($point_add[$item]) : 0;
            $data['余额消耗'][] = isset($point_sub[$item]) ? floatval($point_sub[$item]) : 0;
        }
        foreach ($data as $key => $item) {
            $series[] = [
                'name' => $key,
                'data' => $item,
                'type' => 'line',
            ];
        }
        return compact('xAxis', 'series');
    }

    /**
     * 余额来源
     * @param $where
     * @return array
     */
    public function getChannel($where)
    {
        $bing_xdata = ['系统增加', '用户充值', '佣金提现', '抽奖赠送', '商品退款'];
        $color = ['#64a1f4', '#3edeb5', '#70869f', '#ffc653', '#fc7d6a'];
        $data = ['system_add', 'recharge', 'extract', 'lottery_add', 'pay_product_refund'];
        $bing_data = [];
        foreach ($data as $key => $item) {
            $bing_data[] = [
                'name' => $bing_xdata[$key],
                'value' => $this->dao->sum(['pm' => 1, 'type' => $item, 'time' => $where['time']], 'number', true),
                'itemStyle' => ['color' => $color[$key]]
            ];
        }

        $list = [];
        $count = array_sum(array_column($bing_data, 'value'));
        foreach ($bing_data as $key => $item) {
            $list[] = [
                'name' => $item['name'],
                'value' => $item['value'],
                'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
            ];
        }
        array_multisort(array_column($list, 'value'), SORT_DESC, $list);
        return compact('bing_xdata', 'bing_data', 'list');
    }

    /**
     * 余额类型
     * @param $where
     * @return array
     */
    public function getType($where)
    {
        $bing_xdata = ['系统减少', '充值退款', '购买商品'];
        $color = ['#64a1f4', '#3edeb5', '#70869f'];
        $data = ['system_sub', 'recharge_refund', 'pay_product'];
        $bing_data = [];
        foreach ($data as $key => $item) {
            $bing_data[] = [
                'name' => $bing_xdata[$key],
                'value' => $this->dao->sum(['pm' => 0, 'type' => $item, 'time' => $where['time']], 'number', true),
                'itemStyle' => ['color' => $color[$key]]
            ];
        }

        $list = [];
        $count = array_sum(array_column($bing_data, 'value'));
        foreach ($bing_data as $key => $item) {
            $list[] = [
                'name' => $item['name'],
                'value' => $item['value'],
                'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
            ];
        }
        array_multisort(array_column($list, 'value'), SORT_DESC, $list);
        return compact('bing_xdata', 'bing_data', 'list');
    }

    public function getMoneyList($uid, $type)
    {
        $where = [];
        $where['uid'] = $uid;
        [$page, $limit] = $this->getPageValue();
        if ($type == 1) {
            $where['pm'] = 0;
        } elseif ($type == 2) {
            $where['pm'] = 1;
            $where['not_type'] = ['pay_product_refund'];
        }
        $list = $this->dao->getList($where, $page, $limit);
        $count = $this->dao->count($where);
        $times = [];
        if ($list) {
            foreach ($list as &$item) {
                $item['time'] = $item['time_key'] = $item['add_time'] ? date('Y-m', (int)$item['add_time']) : '';
                $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', (int)$item['add_time']) : '';
            }
            $times = array_merge(array_unique(array_column($list, 'time_key')));
        }
        return ['list' => $list, 'time' => $times, 'count' => $count];
    }

    /**
     * 根据查询用户充值金额
     * @param array $where
     * @param string $rechargeSumField
     * @param string $selectType
     * @param string $group
     * @return float|mixed
     */
    public function getRechargeMoneyByWhere(array $where, string $rechargeSumField, string $selectType, string $group = "")
    {
        switch ($selectType) {
            case "sum" :
                return $this->dao->getWhereSumField($where, $rechargeSumField);
            case "group" :
                return $this->dao->getGroupField($where, $rechargeSumField, $group);
        }
    }

    /**
     * 钱包明细列表
     */
    public function index($params)
    {
        $listParams = [
            'where' => [['uid', '=', $params['user_id']]],
        ];

        if(isset($params['pm']) && in_array($params['pm'], [0,1])) {
            $listParams['where'][] = ['pm', '=', $params['pm']];
        }
        if(!empty($params['add_time_start'])) {
            $listParams['where'][] = ['add_time', '>=', strtotime($params['add_time_start'])];
        }
        if(!empty($params['add_time_end'])) {
            $listParams['where'][] = ['add_time', '<=', strtotime($params['add_time_end'])];
        }

        $listParams['fields'] = ['id', 'uid', 'link_id', 'type', 'number', 'balance', 'pm', 'add_time'];
        $result = $this->getModelList(UserMoney::where('1=1'), $listParams);

        $in_total_amount = $out_total_amount = 0;
        foreach($result['list'] as $key => $val) {
            $pm = $val['pm'] == 1 ? '+' : '-';
            $result['list'][$key]['number'] = $pm . $val['number'];
            $result['list'][$key]['add_time'] = date('Y-m-d H:i:s', $val['add_time']);


            if ($val['type'] == '分销佣金') {
                $result['list'][$key]['user_nickname_name'] = User::where('uid', $val['link_id'])->value('nickname');
            } else {
                $result['list'][$key]['user_nickname_name'] = 0;
            }


            if ($val['pm'] == 1) {
                $in_total_amount += $val['number'];
            } else if ($val['pm'] == 0) {
                $out_total_amount += $val['number'];
            }
        }

        $result['in_total_amount'] = $in_total_amount;
        $result['out_total_amount'] = $out_total_amount;

        return $result;
    }

    /**
     * 钱包明细信息
     */
    public function info($params) {
        $result = [
            'balance' => $params['user']['now_money'] ?? '0.00',//余额
            'settled_amount' => AgentMoneyLog::where('user_id', $params['user']['uid'])->where('cal_status', 1)->sum('money') ?? '0.00',
            'pending_settlement_amount' => $this->getUserPendingSettlementAmount($params['user']),
        ];

        return $result;
    }

    /**
     * 确认提现
     */
    public function confirmCash($params)
    {
        $user = $params['user'];
        $bank_id = $params['bank_id'];

        $bank = AgentBankCard::where('user_id', $user->uid)->where('id', $bank_id)->find();
        if (!$bank) {
            throw new ApiException(31019);
        }

        Db::transaction(function () use ($params, $user, $bank) {
            //判断余额是否足够
            $now_money = $user->now_money;
            // 获取待结算金额
            $pending_settlement_amount = $this->getUserPendingSettlementAmount($user);
            // 获取冻结金额
            $frozen_amount = $this->getUserFrozenAmount($user);
            // 获取可提现金额
            $can_cash_amount = $now_money - $pending_settlement_amount - $frozen_amount;

            if(bcsub($can_cash_amount, $params['amount']) < 0) {
                throw new ApiException(410279);
            }

            // 修改用户表
            $user->now_money = bcsub($user->now_money, $params['amount'], 2);
            $user->frozen_amount = bcadd($user->now_money, $params['amount'], 2);

            $user->save();


            UserExtract::create([
                'uid'   =>  $user->uid,
                'bank_code' => $bank->card_number,
                'extract_type'  =>  'bank',
                'bank_address' => $bank->bank_name,
                'add_time'  =>  time(),
                'balance'   =>  $user->now_money,
                'extract_price' => $params['amount'],
            ]);

        });


        //请求QuicknPay打款逻辑

        //写入用户余额记录
//        $userMoneyData = [
//            'uid' => $params['user_id'],
//            'type' => '提现',
//            'title' => '用户提现',
//            'number' => $params['amount'],
//            'balance' => bcsub($balance, $params['amount'], 2),
//            'pm' => 0,
//            'mark' => '时间：' . date('Y-m-d H:i:s') . '进行提现，提现金额' . $params['amount']
//        ];
//
//        $umId = UserMoney::insertGetId($userMoneyData);
//        if(!$umId) {
//            throw new ApiException(100005);
//        }
    }

    /**
     *
     * 提现界面信息
     * @param $user
     * @return array
     */
    public function cashInfo($user)
    {
        if (!$user) return [];

        $now_money = $user->now_money; // 用户余额
        // 获取待结算金额
        $pending_settlement_amount = $this->getUserPendingSettlementAmount($user);
        // 获取冻结金额
        $frozen_amount = $this->getUserFrozenAmount($user);
        // 获取可提现金额
        $can_cash_amount = $now_money - $pending_settlement_amount - $frozen_amount;
        // 获取手续费
        $service_charge = bcmul($can_cash_amount, 0.06, 2);

        $bank = AgentBankCard::where('user_id', $user->uid)->field('id,account_name, card_number, bank_name')->select();

        return [
            'service_charge'   => (float)$service_charge,
            'min_cash_amount'   =>  100.00,
            'can_cash_amount'   =>  (float)$can_cash_amount,
            'fact_get_amount'   =>  (float)($can_cash_amount - $service_charge),
            'bank'  =>  $bank
        ];
    }

    public function getOrderList($user, $type, $limit)
    {
        $money_log = AgentMoneyLog::with(['order', 'relatedLogs'])->where('user_id', $user->uid)
            ->where('cal_status',  $type)->group('order_id')->paginate($limit);

        $list = [];
        foreach ($money_log as $log) {

            $products = [];
            foreach ($log->relatedLogs as $item) {

                $orderItem = StoreOrderItem::where('order_id', $item->order_id)->where('product_attr_value_unique' ,$item->product_att_value_unique)->find();
                $attrInfo = $item->attrInfo ?? null;
                $products[] = [
                    'image' => ($attrInfo) ? $attrInfo->image : '',
                    'store_name'    =>  ($attrInfo && $attrInfo->product) ? $attrInfo->product->store_name : '',
                    'suk'=>  $attrInfo ? $attrInfo->suk : '',
                    'fact_price'    =>  $orderItem ? $orderItem->fact_price : '',
                    'nums'   =>  $orderItem ? $orderItem->nums : '',
                ];
            }

            $list[] = [
                'buyer_nickname' =>  $log->buyer ? $log->buyer->nickname : '',
                'create_time'   =>  $log->order ? Carbon::parse($log->order->add_time)->format('Y-m-d H:i:s') : '',
                'money' =>  $log->money,
                'products'   => $products
            ];
        }

        // --

        return ['list' => $list, 'count' => $money_log->count()];

    }

    /**
     *
     * 获取待结算金额
     * @param $user
     * @return int
     */
    private function getUserPendingSettlementAmount($user)
    {
        if (!$user) return 0;
        return 0;
    }

    /**
     * 获取冻结金额
     * @param $user
     * @return int
     */
    private function getUserFrozenAmount($user)
    {
        if (!$user) return 0;
        return $user->forzen_amount;
    }

}
